package com.yzr.common.util;

import com.yzr.common.constants.ServiceConstants;
import org.springframework.util.StringUtils;

/**
 * 特殊字符转义
 *
 * @author yx-0173
 */
public class CharEscapeUtil {
    /**
     * 特殊字符转义工具类
     *
     * @param charString 字符串
     * @return 转义后的字符串
     */
    public static String charEscape(String charString) {
        if (!StringUtils.hasLength(charString)) {
            return charString;
        }
        charString = charString.trim();
        if (charString.contains(ServiceConstants.BACKSLASH)) {
            charString = charString.replaceAll("\\\\", "\\\\");
        }
        if (charString.contains(ServiceConstants.PERCENT_SIGN)) {
            charString = charString.replaceAll("%", "\\\\%");
        }
        if (charString.contains(ServiceConstants.UNDERSCORE)) {
            charString = charString.replaceAll("_", "\\\\_");
        }
        return charString;
    }
}
